package com.xt.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.back.dto.RelationMappingDto;
import com.xt.back.entity.RelationMapping;
import com.xt.back.mapper.RelationMapper;
import com.xt.back.mapper.RelationMappingMapper;
import com.xt.back.mapper.UserMapper;
import com.xt.back.service.IRelationMappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 用户关系关联表 服务实现类
 * </p>
 *
 * @author leotong
 * @since 2022-04-03
 */
@Service
@AllArgsConstructor
public class RelationMappingServiceImpl extends ServiceImpl<RelationMappingMapper, RelationMapping> implements IRelationMappingService {

    private final UserMapper userMapper;

    private final RelationMapper relationMapper;

    private final RelationMappingMapper relationMappingMapper;

    @Override
    public Page<RelationMappingDto> getRelationMappingByPage(RelationMapping relationMapping) {
        Page<RelationMapping> page=new Page<>();
        Page<RelationMappingDto> result=new Page<>();
        QueryWrapper<RelationMapping> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(relationMapping.getRelationId()!=null&&!Objects.equals(relationMapping.getRelationId(),""),RelationMapping::getRelationId,relationMapping.getRelationId())
                .eq(relationMapping.getUserFirstId()!=null&&!Objects.equals(relationMapping.getUserFirstId(),""),RelationMapping::getUserFirstId,relationMapping.getUserFirstId())
                .eq(relationMapping.getUserSecondId()!=null&&!Objects.equals(relationMapping.getUserSecondId(),""),RelationMapping::getUserSecondId,relationMapping.getUserSecondId());
        Page<RelationMapping> relationMappingPage = relationMappingMapper.selectPage(page, queryWrapper);
        result.setTotal(relationMappingPage.getTotal());
        result.setRecords(convert(relationMappingPage.getRecords()));
        return result;
    }

    @Override
    public List<RelationMappingDto> getAllRelationMapping() {
        return convert(this.list());
    }

    public List<RelationMappingDto> convert(List<RelationMapping> relationMappings){
        List<RelationMappingDto> result=new ArrayList<>();
        for (RelationMapping relationMapping : relationMappings) {
            RelationMappingDto relationMappingDto=new RelationMappingDto();
            BeanUtils.copyProperties(relationMapping,relationMappingDto);
            relationMappingDto.setFirstUser(userMapper.selectById(relationMapping.getUserFirstId()));
            relationMappingDto.setSecondUser(userMapper.selectById(relationMapping.getUserSecondId()));
            relationMappingDto.setRelation(relationMapper.selectById(relationMapping.getRelationId()));
            result.add(relationMappingDto);
        }
        return result;
    }
}
